package com.atguigu.paymentdemo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.paymentdemo.entity.CourseTeacher;
import com.atguigu.paymentdemo.service.CourseTeacherService;
import com.atguigu.paymentdemo.util.AjaxResult;
import com.atguigu.paymentdemo.util.AliyunOssUtil;
import com.atguigu.paymentdemo.vo.CourseTeacherVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jodd.util.StringUtil;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author wulifeng
 */
@Api(tags = "讲师表Controller")
@RestController
@RequestMapping("courseTeacher")
@AllArgsConstructor
public class CourseTeacherController {

	private final CourseTeacherService courseTeacherService;

	/**
	 * 分页查询
	 *
	 * @param page          分页对象
	 * @param courseTeacher 讲师
	 * @return /
	 */
	@ApiOperation(value = "分页查询")
	@GetMapping(value = "/page", produces = {"application/json;charset=UTF-8"})
	public AjaxResult getCourseTeacherPage(Page<CourseTeacher> page, CourseTeacher courseTeacher) {
		LambdaQueryWrapper<CourseTeacher> lqw = Wrappers.lambdaQuery();
		lqw.like(StringUtil.isNotBlank(courseTeacher.getTeacherName()), CourseTeacher::getTeacherName, courseTeacher.getTeacherName());
		return AjaxResult.success(courseTeacherService.page(page, lqw));
	}

	/**
	 * 下拉选项
	 *
	 * @return /
	 */
	@ApiOperation(value = "下拉选项")
	@GetMapping(value = "/list", produces = {"application/json;charset=UTF-8"})
	public AjaxResult getCourseTeacherList() {
		List<CourseTeacher> list = courseTeacherService.list();
		List<CourseTeacherVo> voList = list.stream().map( courseTeacher -> BeanUtil.toBean(courseTeacher,CourseTeacherVo.class)).collect(Collectors.toList());
		return AjaxResult.success(voList);
	}

	/**
	 * 通过id查询讲师详情
	 *
	 * @param id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询讲师详情")
	@GetMapping(value = "/{id}", produces = {"application/json;charset=UTF-8"})
	public AjaxResult getById(@PathVariable("id") String id) {
		return AjaxResult.success(courseTeacherService.getById(id));
	}

	/**
	 * 新增讲师
	 *
	 * @param courseTeacher 讲师
	 * @return R
	 */
	@ApiOperation(value = "新增讲师")
	@PostMapping(produces = {"application/json;charset=UTF-8"})
	public AjaxResult save(@RequestBody CourseTeacher courseTeacher) {
		return AjaxResult.success(courseTeacherService.save(courseTeacher));
	}

	/**
	 * 修改讲师
	 *
	 * @param courseTeacher 讲师
	 * @return R
	 */
	@ApiOperation(value = "修改讲师")
	@PutMapping(produces = {"application/json;charset=UTF-8"})
	public AjaxResult updateById(@RequestBody CourseTeacher courseTeacher) {
		return AjaxResult.success(courseTeacherService.updateById(courseTeacher));
	}

	/**
	 * 通过id删除讲师
	 *
	 * @param id /
	 * @return R
	 */
	@ApiOperation(value = "通过id删除讲师")
	@DeleteMapping(value = "/{id}", produces = {"application/json;charset=UTF-8"})
	public AjaxResult removeById(@PathVariable Long id) {
		return AjaxResult.success(courseTeacherService.removeById(id));
	}
}
